package com.demo.controller;

import com.demo.service.DeptService;
import com.dove.pojo.Dept;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    DeptService deptService;

    @PostMapping("/add")
    public boolean add(Dept dept) {
        return deptService.addDept(dept);
    }

    @GetMapping("/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Dept get(@PathVariable("id") Long id) {
        Dept dept = deptService.queryById(id);
        if (dept == null) {
            throw new RuntimeException("不使用Hystrix时异常");
        }
        return dept;
    }

    @GetMapping("/list")
    public List<Dept> queryAll() {
        return deptService.queryAll();
    }
    // 备用方法
    public Dept hystrixGet(@PathVariable("id") Long id) {
        Dept dept = new Dept().setDeptno(id)
                .setDname("id不存在 from Hystrix")
                .setDb_source("服务熔断");
        return dept;
    }
}

